<?php
	class AdvertisingAction extends Action{
		public function index()
		{
			if(!$this->isPost()){
				$this->display();
			}else{
				//获得图片
				$pic = $_FILES['a_img'];
				//获得图的临时路径
				$tmpname = $pic['tmp_name'];
				//获得图片类型
				$type = $pic['type'];
				$type = explode('/',$type);
				$type_1 = $type[0];
				//重新给图片起名
				$picture='guanggao_'.time().rand(1000,9999).".".$type[1];
				//上传图片时 的报错
				$error=$pic['error'];
				//图片上传的路径
				$newname=ROOT_DIR.DS.'Public/Upload/'.$picture;
				if(@is_uploaded_file($tmpname) && $type_1 == 'image' && $error == '0'){
					@move_uploaded_file($tmpname,$newname);
				}
				$data['location'] = $_POST['location'];
				$data['place'] = $_POST['place'];
				$data['img'] = $picture;
				$result = M("advertising")->data($data)->add();
				if($result){
					$this->assign("jumpUrl","__URL__/index");
					$this->assign("waitSecond",3);
					$this->success("广告添加成功！！！");

				}else{
					$this->assign("jumpUrl","__URL__/index");
					$this->assign("waitSecond",3);
					$this->error("广告添加失败！！！");
				}
			}
		}
		
		/**
		 * @name: list
		 * @desc: Advertising list
		 */
		public function lists()
		{
			import("ORG.Util.Page");
			$count   = M("advertising")->count();
			$Page    = new Page($count,10);
			$show    = $Page->show();
			$list =  M('advertising')->order('id')->limit($Page->firstRow.','.$Page->listRows)->select();
			$this->assign("page",$show);
			$this->assign("adlist",$list);
			$this->display('adlist');
		}
	
		/**
		 * @name: delAd
		 * @desc: 删除广告
		 */
		public function delAd()
		{
			$id = intval($_REQUEST['id']);
			$state = M('advertising')->where('id='.$id)->delete();
			if($state>0)
			{
				echo 'success';
			}
			else
			{
				echo '0';
			}
			return ;
		}
		
		/**
		 * @name : editAd
		 * @desc : 编辑广告
		 */
		public function editAd()
		{
			if ( $this->isPost() ) 
			{
				$pic 		= $_FILES['a_img'];//获得图片
				if(!empty($pic))
				{
					//获得图的临时路径
					$tmpname 	= $pic['tmp_name'];
					//获得图片类型
					$type 		= $pic['type'];
					$type 		= explode('/',$type);
					$type_1 	= $type[0];
					//重新给图片起名
					$picture	= 'guanggao_'.time().rand(1000,9999).".".$type[1];
					//上传图片时 的报错
					$error		= $pic['error'];
					//图片上传的路径
					$newname	= ROOT_DIR.DS.'Public/Upload/'.$picture;
					if(@is_uploaded_file($tmpname) && $type_1 == 'image' && $error == '0')
					{
						@move_uploaded_file($tmpname,$newname);
					}
					$data['img'] = $picture;
				}
				$data['location'] 	= $_POST['location'];
				$data['place'] 		= $_POST['place'];			
				$data['location'] 	= $_POST['location'];
				$data['place'] 		= $_POST['place'];
				$data['img'] 		= $picture;
				$result 	= 	M("advertising")->where('id='.$_POST['id'])->data($data)->save();
				
				if($result)
				{
					$this->assign("jumpUrl","__URL__/lists");
					$this->assign("waitSecond",3);
					$this->success("修改成功！！！");
				
				}else{
					$this->assign("jumpUrl","__URL__/lists");
					$this->assign("waitSecond",3);
					$this->error("修改失败！！！");
				}
			}
			else
			{
				$id = intval($_REQUEST['id']);
				$info = M('advertising')->where('id='.$id)->select();
				$this->assign('adinfo',$info[0]);
				$this->display('editad');
			}
		}
		
	}

?>